﻿using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Kdy.AuthCenter.Admin.EntityFramework.Shared.Constants;
using Kdy.AuthCenter.Admin.EntityFramework.Shared.Entities.Identity;
using System.ComponentModel.DataAnnotations.Schema;
using Kdy.AuthCenter.Admin.EntityFramework.Shared.ValueGenerates;

namespace Kdy.AuthCenter.Admin.EntityFramework.Shared.DbContexts
{
    public class AdminIdentityDbContext : IdentityDbContext<UserIdentity, UserIdentityRole, long, UserIdentityUserClaim, UserIdentityUserRole, UserIdentityUserLogin, UserIdentityRoleClaim, UserIdentityUserToken>
    {
        public AdminIdentityDbContext(DbContextOptions<AdminIdentityDbContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            ConfigureIdentityContext(builder);
        }

        private void ConfigureIdentityContext(ModelBuilder builder)
        {
            builder.Entity<UserIdentityRole>()
                .ToTable(TableConsts.IdentityRoles)
                .Property(a => a.Id)
                .IsRequired()
                .HasValueGenerator<GenerateForLong>()
                .ValueGeneratedNever();
            builder.Entity<UserIdentityRoleClaim>()
                .ToTable(TableConsts.IdentityRoleClaims);
            builder.Entity<UserIdentityUserRole>()
                .ToTable(TableConsts.IdentityUserRoles);

            builder.Entity<UserIdentity>()
                .ToTable(TableConsts.IdentityUsers)
                .Property(a => a.Id)
                .IsRequired()
                .HasValueGenerator<GenerateForLong>()
                .ValueGeneratedNever(); ;
            builder.Entity<UserIdentityUserLogin>().ToTable(TableConsts.IdentityUserLogins);
            builder.Entity<UserIdentityUserClaim>()
                .ToTable(TableConsts.IdentityUserClaims);
            builder.Entity<UserIdentityUserToken>()
                .ToTable(TableConsts.IdentityUserTokens);
        }
    }
}







